<?php
/**
 * Created by PhpStorm.
 * User: xuyiping
 * Date: 2018/7/5
 * Time: 11:26
 */
namespace app\Http\Controllers\Api;

use App\Http\Controllers\Admin\GoldController;
use App\Http\Controllers\Controller;
use App\Model\GoldRecordModel;
use App\Model\SignModel;
use Symfony\Component\HttpFoundation\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Api\ApiMainController;
use App\Model\GoldBeanModel;
class ApiSignController extends ApiMainController{

    public function index(){
        if ($this->_defalutJson['code'] == -1 && empty($this->userId)) {
            return response()->json(array(
                'code' => 404,
                'message' => $this->_defalutJson['message'],
            ));
        }

        //获取当前金豆数
        $now_gold_bean = GoldBeanModel::select('iNowGoldBean')->where('iUserId', $this->userId)->first();
        $gold_bean_num = 0;
        if (!empty($now_gold_bean) && $now_gold_bean->iNowGoldBean > 0) {
            $gold_bean_num = $now_gold_bean->iNowGoldBean;
        }


        //返回签到天数
        $user_data = DB::table('webuser')->where('iUUID',$this->uuid)->first();

        //step 验证今天是否签到
        $result =DB::table('sign')->where(array('iUserId'=>$this->userId,'iTime'=>strtotime(date("Y-m-d"))))->first();
        if(!empty($result)){
            if($result->igetGold==2){
                return response()->json(
                    Array("code"=>400,
                        "message"=>"您今天已经签到过",
                        "data"=>Array(
                            "gold"=>$gold_bean_num,
                            "uuid"=>$this->uuid,
                            "day" =>empty($user_data) ? '' : $user_data->iSignday,
                            "time" =>$result->dtModifyTime,
                        ),
                    )
                );
            }elseif($result->igetGold==1){
                $num = DB::table('sign')
                    ->where(array('iUserId'=>$this->userId,'iTime'=>strtotime(date("Y-m-d"))))
                    ->update(array('dtModifyTime'=>date("Y-m-d H:i:s")));
                //判断是否加金币
                if($gold_bean_num < 110){
                    $gold = new GoldController();
                    $gold->exChangeGold(110,$this->userId,4); //签到获取
                    $num = DB::table('sign')->where("iSeqId",'=',$result->iSeqId)->increment('igetGold');
                    $gold_bean_num+=110;
                }

            }else{
                $num = DB::table('sign')
                    ->where(array('iUserId'=>$this->userId,'iTime'=>strtotime(date("Y-m-d"))))
                    ->update(array('dtModifyTime'=>date("Y-m-d H:i:s")));
                //判断是否加金币
                if($gold_bean_num < 110){
                    $gold = new GoldController();
                    $gold->exChangeGold(110,$this->userId,4); //签到获取
                    $num = DB::table('sign')->where("iSeqId",'=',$result->iSeqId)->increment('igetGold');
                    $gold_bean_num+=110;
                }

            }
            return response()->json(Array(
                "code"=>200,
                "message"=>"签到成功",
                "data"=>Array(
                    "gold"=>$gold_bean_num,
                    "uuid"=>$this->uuid,
                    "day" =>empty($user_data) ? 0 : $user_data->iSignday,
                    "time" =>date("Y-m-d H:i:s"),
                ),

            ));
        }
        $data['iUserId'] = $this->userId;
        $data['iSignIp'] = $_SERVER['REMOTE_ADDR'];
        $data['dtModifyTime'] = date("Y-m-d H:i:s");
        $data['iTime'] = strtotime(date("Y-m-d"));

       /* $data['time'] = date("Y-m-d");*/
        $groups = DB::table('sign')->insertGetId($data);
        //返回数组

        if($groups){
            //人员表签到数加1
            $num = DB::table('webuser')
                ->where("iSeqId",'=',$this->userId)
                ->increment('iSignday');

            //判断是否加金币
            if($gold_bean_num < 110){
                $gold = new GoldController();
                $gold->exChangeGold(110,$this->userId,4); //签到获取
                $num = DB::table('sign')->where("iSeqId",'=',$groups)->increment('igetGold');
            }

            //获取当前金豆数
            $now_gold_bean = GoldBeanModel::select('iNowGoldBean')->where('iUserId', $this->userId)->first();
            $gold_bean_num = 0;
            if (!empty($now_gold_bean) && $now_gold_bean->iNowGoldBean > 0) {
                $gold_bean_num = $now_gold_bean->iNowGoldBean;
            }

            return response()->json(Array(
                "code"=>200,
                "message"=>"签到成功",
                "data"=>Array(
                    "gold"=>$gold_bean_num,
                    "uuid"=>$this->uuid,
                    "day" =>empty($user_data) ? '' : ($user_data->iSignday)+1,
                    "time" =>date("Y-m-d H:i:s"),
                ),

            ));
        }
        return response()->json(Array(
            "code"=>400,"data"=>Array(),
            "message"=>"未知错误，请重试"
        ));
    }
    //签到获取金豆
   /* function addGold(){

        if ($this->_defalutJson['code'] == -1 && empty($this->userId)) {
            return response()->json(array(
                'code' => 400,
                'message' => $this->_defalutJson['message'],
            ));
        }

        $result =SignModel::where(array('iUserId'=>$this->userId,'iTime'=>strtotime(date("Y-m-d"))))->get()->toArray();
        if(!empty($result)){
            if($result[0]['igetGold']==0){
                //还没有获取金豆
                $gold = new GoldController();
                $gold->exChangeGold(1000,$this->userId,4); //签到获取
                SignModel::where("iSeqId","=",$result[0]['iSeqId'])->update(array('igetGold'=>'1'));//消
                //$num =SignModel::where("iSeqId",'=',$result[0]['iSeqId'])->update(array("igetGold"=>"1"));
                return response()->json(array(
                    'code' => 200,
                    'message' => "获取金豆成功",
                    'data'=>['uuid'=>$this->uuid]
                ));
            }else{

                return response()->json(array(
                    'code' => 400,
                    'message' => "您已经获取成功，请勿重复提交",
                ));
            }
        }else{
            return response()->json(array(
                'code' => 404,
                'message' => "对不起，没有找到数据",
                'data'=>['uuid'=>$this->uuid]
            ));
        }
    }*/
    
}